package one.stand.controller;

import com.jmp.base.ChatApi;
import com.jmp.base.dto.*;
import com.jmp.chat.*;
import lombok.extern.slf4j.Slf4j;
import one.stand.advice.ResultEnum;
import one.stand.dto.ConvertUtils;
import one.stand.mapper.UserChatMapperExt;
import one.stand.model.ResultModel;
import one.stand.model.UserChat;
import one.stand.service.ChatService;
import one.stand.util.AssertUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * @author cjr
 */
@RestController
@Slf4j
public class ChatController implements ChatApi {
    @Autowired
    private ChatService chatService;
    @Autowired
    private UserChatMapperExt userChatMapperExt;
    @Autowired
    private ChatClient chatClient;

    @Override
    public ResultModel init() {

        chatService.init();

        return ResultModel.success();
    }

    @Override
    public ResultModel save(@RequestBody ChatSaveDto request) {

        chatService.save(request.getUserId());

        return ResultModel.success();
    }

    @Override
    public ResultModel<ChatInfoVo> info(@RequestBody ChatInfoDto request) {
        UserChat userChat = chatService.get(request.getUserId());

        ChatInfoVo vo = ChatInfoVo.builder()
                .chatUserId(userChat.getChatUserId())
                .chatPassword(chatClient.getPwd(userChat.getChatUserId()))
                .build();
        return ResultModel.success(vo);
    }

    @Override
    public ResultModel<ChatGroupSaveVo> groupSave(@RequestBody ChatGroupSaveDto request) {

        String groupId = chatClient.saveGroup(ChatGroupSaveRequest.builder().name(request.getName()).build());
        AssertUtil.nonNull(groupId, ResultEnum.FAIL);
        ChatGroupSaveVo vo = ChatGroupSaveVo.builder().groupId(groupId).build();
        return ResultModel.success(vo);
    }

    @Override
    public ResultModel groupRemove(@RequestBody ChatGroupRemoveDto request) {

        chatClient.groupRemove(ChatGroupRemoveRequest.builder().groupId(request.getGroupId()).build());

        return ResultModel.success();
    }

    @Override
    public ResultModel<ChatGroupInfoVo> groupInfo(@RequestBody ChatGroupInfoDto request) {

        ChatGroupInfoResponse response = chatClient.groupInfo(ChatGroupInfoRequest.builder().groupId(request.getGroupId()).build());
        if (Objects.nonNull(response)) {
            List<String> memberPhotoList = ConvertUtils.list(response.getMemberList(), user -> {
                UserChat userChat = userChatMapperExt.selectByChatUserId(user);
                return userChat.getPhoto();
            });
            String memberPhotos = StringUtils.join(memberPhotoList, ",");
            ChatGroupInfoVo vo = ChatGroupInfoVo.builder()
                    .groupId(request.getGroupId())
                    .memberNum(response.getMemberNum())
                    .memberPhotos(memberPhotos)
                    .build();
            return ResultModel.success(vo);
        }

        return ResultModel.fail();
    }

    @Override
    public ResultModel groupMsgSystem(@RequestBody ChatGroupMsgSystemDto request) {

        chatService.groupSystemMsg(request.getGroupId(), request.getContent());

        return ResultModel.success();
    }

    @Override
    public ResultModel msgSystem(@RequestBody ChatMsgSystemDto request) {
        chatService.msgSystem(request);
        return ResultModel.success();
    }
}
